package top.jandmla.imagetest.test;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * <p>
 *
 * </p>
 */
public class Answer {
    public static void main(String[] args) {

//        int[] answer = answer(new int[]{359,329,594,957,830,46,158,11,12,13,14,15,16,17,18});
        int[] answer = getArray(-5);
        answer = sort(answer);
        System.out.println("getIndex(new int[]{4,3,2,15}) = " + getIndex(new int[]{4, 3, 2, 15}));
        System.out.println("getAnswer(new int[]{1,2,3,4}) = " + getAnswer(new int[]{1, 2, 3, 4}));

        System.out.println("answer.length = " + answer.length);
        for (int i : answer) {
            System.out.print(i+"   ");
        }
    }

    public static int getAnswer(int[] x){
        int len = x.length;
        int mid = len/2;
        if (len == 0)
            return 4;
        if (len >= 5 && len %2 == 1)
            return x[mid-2]+x[mid-1]+x[mid]+x[mid+1]+x[mid+2];
        else if (x.length >= 4)
            return x[mid-2]+x[mid-1]+x[mid]+x[mid+1];
        //题不严谨
        else
            return 0;
    }
    public static int[] getArray(int i){
        int[] ints = new int[11];
        if ( i >= 0){
            ints[10] = 8;
            for (int j = 9; j >= 0; j--) {
                ints[j] = ints[j+1]+i;
            }
        }else {
            ints[0] = 8;
            for (int j = 1; j < 11; j++) {
                ints[j] = ints[j-1]+i;
            }
        }
        return ints;
    }
    public static int getIndex(int[] x){
        Arrays.sort(x);
        int total = 38;
        if (x[0] > total)
            return 0;
        if (x[x.length-1] == total)
            return x.length-1;
        if (x[x.length-1] < total)
            return x.length;
        for (int i = 0; i < x.length-1; i++) {
            if (x[i] == total)
                return i;
            if (x[i]<total && x[i+1]>total)
                return i+1;
        }
        return -1;
    }

    public static int[] sort(int[] x){
        for (int i = 0; i < x.length; i++) {
            for (int j = i; j < x.length; j++) {
                if (x[i] > x[j]){
                    int tem = x[i];
                    x[i] = x[j];
                    x[j] = tem;
                }
            }
        }
        return x;
    }






//    public static int[] answer(int[] x){
//        int n = x.length;
//        Stack<Integer> stack = new Stack<>();
//        if (n > 7)
//            n = 7;
//        for (int i = 0; i < n; i++) {
//            stack.push(x[i]);
//        }
//        int c = 0;
//        if (n <= 4){
//            c = getChar(new int[0]);
//        }else {
//            for (int i = 0; i < 4; i++) {
//                stack.pop();
//            }
//            int[] ints = new int[n - 4];
//            for (int i = 0; i < n - 4; i++) {
//                ints[i] = stack.pop();
//            }
//            c = getChar(ints);
//        }
//        return getArray(c);
//    }
//
//    public static int getChar(int[] x){
//        System.out.println("x.length = " + x.length);
//        if (x.length == 0)
//            return 0;
//        if (x.length == 1)  //==
//            return x[0];
//        if (x.length >= 8)
//            return x[8-1];
//        if (x.length >= 8/2)
//            return x[8/2-1];
//        return x[8/4-1];
//    }
//
//    public static int[] getArray(int i){
//        int[] ints = new int[15];
//        if ( i >= 0){
//            ints[14] = 51;
//            for (int j = 13; j >= 0; j--) {
//                ints[j] = ints[j+1]+i;
//            }
//        }else if (i <0){
//            ints[0] = 51;
//            for (int j = 1; j < 14; j++) {
//                ints[j] = ints[j-1]+i;
//            }
//        }
//        Arrays.parallelSort(ints);
//        return ints;
//    }
}
